package com.jiai.cloud.community.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.community.entity.TComment;
import com.jiai.cloud.community.entity.TTopic;
import com.jiai.cloud.community.entity.ToPicSearch;
import com.jiai.cloud.community.service.ITCommentService;
import com.jiai.cloud.community.service.ITTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-06-07
 */
@RestController
@RequestMapping("/api/v1/topic")
public class TTopicController {
    @Autowired
    private ITTopicService itTopicService;
    @Autowired
    private ITCommentService iTCommentService;



    @RequestMapping(value = "/findByAll" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object findByAll() throws ServiceException {
        try {
               return RestResultGenerator.genSuccessResult(itTopicService.countTopicIndexData());
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/addTopic",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object addTopic(TTopic tTopic)throws ServiceException {
        try{
            tTopic.setTime(new Date());
            itTopicService.insert(tTopic);
            return RestResultGenerator.genSuccessResult("插入成功");
        }catch (Exception e){
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    @RequestMapping(value = "/listTopic" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object selectList(ToPicSearch search) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itTopicService.selecttopicByAllForJOINUser(search));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }
    //单连表删除
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteId( Integer id) throws ServiceException{
        try{
            itTopicService.delete(new EntityWrapper<TTopic>().eq("id", id));
            iTCommentService.delete(new EntityWrapper<TComment>().eq("topicId", id));
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //批量连表删除
    @RequestMapping(value = "/listDel", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteList(String  ids) throws ServiceException{
        try{
            String[] idArray=ids.split(",");
            List<Integer> idsList=new ArrayList<>();
            for(String id:idArray){
                idsList.add(Integer.valueOf(id));
            }
            itTopicService.delete(new EntityWrapper<TTopic>().in("id", idsList.toArray()));
            iTCommentService.delete(new EntityWrapper<TComment>().in("topicId", idsList.toArray()));
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    @RequestMapping( value = "/updateTopic" ,method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public  Object updateTopic(TTopic tTopic) throws ServiceException {
        try {
            itTopicService.update(tTopic,new EntityWrapper<TTopic>().eq("id" ,tTopic.getId()));
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

}
